//
//  LogViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "LogViewController.h"
#import "TAMacro.h"
#import "TextView.h"
#import "LoginViewController.h"
#import "PersonalViewController.h"
#import "ZWYNetTool.h"

#import "FirstLoginFirst.h"
#import "FirstLoginData.h"
#import "FirstLoginItems.h"
#import "FirstLoginImage.h"

#import "SecLoginFirst.h"
#import "SecLoginData.h"
#import "SecLoginItems.h"
#import "SecLoginImage.h"

@interface LogViewController ()
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *passWordTextField;
@property (nonatomic, strong) UIButton *logButton;

@property (nonatomic, strong) FirstLoginFirst *first;
@property (nonatomic, strong) SecLoginFirst *secFirst;

@end

@implementation LogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}
// 登录请求
- (void)createData {
    NSLog(@"%@", self.phoneTextField.text);
    NSString *str1 = @"http://mobile.iliangcang.com/login/userLogin?app_key=Android&password=";
    NSString *str2 = @"&sig=27E5CDCAEC11143DCF4A3354BEC0DCDA%7C356970820598468&username=";
    NSString *str3 = @"&v=1.0";
    NSString *str = [[[[str1 stringByAppendingString:self.passWordTextField.text] stringByAppendingString:str2] stringByAppendingString:self.phoneTextField.text] stringByAppendingString:str3];
    NSLog(@"%@", str);
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Connection": @"Keep-Alive", @"User-Agent": @"okhttp/3.2.0 Paros/3.2.13"};
    
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.first = [FirstLoginFirst modelWithDic:result];
        NSLog(@"%@,   %@", self.first.data.items.user_token, self.first.data.items.user_id);
        [self createSecData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}

- (void)createSecData {
    NSString *str = [[[[@"http://mobile.iliangcang.com/user/myAccount?app_key=Android&is_login=1&sig=27E5CDCAEC11143DCF4A3354BEC0DCDA%7C356970820598468&uid=" stringByAppendingString:self.first.data.items.user_id] stringByAppendingString:@"&user_token="] stringByAppendingString:self.first.data.items.user_token] stringByAppendingString:@"&v=1.0"];
     NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Connection": @"Keep-Alive", @"User-Agent": @"okhttp/3.2.0 Paros/3.2.13"};
    NSLog(@"%@", str);
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.secFirst = [SecLoginFirst modelWithDic:result];
        NSLog(@"%@", self.secFirst.data.items.nickname);
        
        NSNotification *notice = [NSNotification notificationWithName:@"sendValue" object:nil userInfo:@{@"text": self.secFirst.data.items}];
        [[NSNotificationCenter defaultCenter] postNotification:notice];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *dataDic = [NSDictionary dictionaryWithObjects:@[self.phoneTextField.text, self.passWordTextField.text] forKeys:@[@"userName", @"passWord"]];
        [userDefaults setObject:dataDic forKey:@"myDic"];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}

- (void)createView {
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
   
    self.backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImageView.image = [UIImage imageNamed:@"34.jpg"];
    [self.view addSubview:self.backImageView];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(WIDTH - 40, 35, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"6"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 130, WIDTH - 40, 50)];
    self.phoneTextField.backgroundColor = [UIColor whiteColor];
    self.phoneTextField.clearButtonMode = UITextFieldViewModeAlways;
    UIImage *phoneImage = [UIImage imageNamed:@"11"];
    UIView *phoneImageView = [TextView createThisViewWithImage:phoneImage];
    
    self.phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    self.phoneTextField.leftView = phoneImageView;
    
    self.phoneTextField.placeholder = @"请输入手机号";
    [self.view addSubview:self.phoneTextField];
    
    self.passWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, WIDTH - 40, 50)];
    self.passWordTextField.backgroundColor = [UIColor whiteColor];
    self.passWordTextField.leftView.backgroundColor = [UIColor redColor];
    self.passWordTextField.clearButtonMode = UITextFieldViewModeAlways;
    UIImage *passWordImage = [UIImage imageNamed:@"12"];
    UIView *passWordImageView = [TextView createThisViewWithImage:passWordImage];
    
    self.passWordTextField.leftViewMode = UITextFieldViewModeAlways;
    self.passWordTextField.leftView = passWordImageView;
    self.passWordTextField.placeholder = @"请输入密码";
    [self.view addSubview:self.passWordTextField];
    
    self.logButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.logButton.frame = CGRectMake(20, 270, WIDTH - 40, 50);
    self.logButton.backgroundColor = [UIColor blackColor];
    [self.logButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.logButton setTintColor:[UIColor whiteColor]];
       [self.logButton addTarget:self action:@selector(logClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.logButton];

}
- (void)logClick:(UIButton *)button {
    if ((self.phoneTextField.text.length == 0) || (self.passWordTextField.text.length ==0)) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的用户名和密码" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    } else {
    [self createData];

    [self dismissViewControllerAnimated:YES completion:nil];
    }
}


- (void)backAction:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
